import Exceljs from 'exceljs';
import { saveAs } from 'file-saver';

// 获取导出的excel的列配置
const getExcelColumns = (columns: any[]) => {
  return columns.map((item) => {
    let items: any = {
      header: item.title,
      key: item.dataIndex,
      // 这里是为了自定义表格内容的样式（不包含表头）
      style: {
        // 字体样式
        font: { size: 11 },
        // 边框样式
        border: {
          top: { style: 'thin' },
          left: { style: 'thin' },
          bottom: { style: 'thin' },
          right: { style: 'thin' },
        },
        // 设置水平，垂直对齐方式以及是否换行
        alignment: {
          wrapText: true,
          vertical: 'middle',
          horizontal: 'center',
        },
      },
    };
    items.width = 25;
    return items;
  });
};

// 去除不需要的表头
const initCol = (data: any[]) => {
  const arr: any[] = [];
  data.forEach((el: { key: string; dataIndex: string }) => {
    if (el.key !== 'option' && el.key !== 'mark') {
      arr.push(el);
    }
  });
  return arr;
};

// 导出excel
export const exportToExcel = (columns: any[], dataSource: any[], name: string) => {
  let newColumns = columns.length ? columns.filter((el: any) => !el.hideInTable) : [];
  const workbook = new Exceljs.Workbook();
  //创建一个名字为Sheet1的工作表
  const worksheet: any = workbook.addWorksheet(name);
  // worksheet.properties.defaultRowHeight = 15; // 设置默认行高
  worksheet.columns = getExcelColumns(initCol(newColumns));
  let data = dataSource.filter((el: { dataIndex: string }) => el.dataIndex !== 'option');
  worksheet.addRows(data);
  // 定义表头样式
  let headerStyle = {
    font: {
      name: 'Arial',
      size: 14,
      bold: true,
      color: { argb: '00ffffff' },
    },
    fill: {
      type: 'pattern',
      pattern: 'solid',
      fgColor: { argb: '20a162' },
    },
    alignment: {
      wrapText: true,
      vertical: 'middle',
      horizontal: 'center',
    },
    border: {
      top: { style: 'double' },
      left: { style: 'thin' },
      bottom: { style: 'thin' },
      right: { style: 'thin' },
    },
  };

  // 定义标记表格内容
  let contentStyle = {
    font: {
      color: { argb: '000000' },
    },
    fill: {
      type: 'pattern',
      pattern: 'solid',
      fgColor: { argb: 'fbc82f' },
    },
    alignment: {
      wrapText: true,
      vertical: 'middle',
      horizontal: 'center',
    },
  };

  // 填充表头的样式
  for (let i = 1; i <= worksheet.columnCount; i++) {
    let cell = worksheet.getCell(1, i);
    cell.font = headerStyle.font;
    cell.fill = headerStyle.fill;
    cell.alignment = headerStyle.alignment;
    cell.border = headerStyle.border;
  }

  // 填充
  for (let i = 1; i <= worksheet.columnCount; i++) {
    for (let j = 0; j < dataSource.length; j++) {
      if (dataSource[j].mark === '1') {
        let cell = worksheet.getCell(j + 2, i);
        cell.font = contentStyle.font;
        cell.fill = contentStyle.fill;
        cell.alignment = contentStyle.alignment;
      }
    }
  }

  workbook.xlsx.writeBuffer().then((buffer) => {
    let blob = new Blob([buffer], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    });
    saveAs(blob, name + '.xlsx');
  });
};
